package com.rockxin.cat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.PagerAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.bean.BidDetailsBean;
import com.rockxin.cat.bean.UseBean;
import com.rockxin.cat.customview.NoScrollViewPager;
import com.rockxin.cat.fragment.BIdInfoFragmentFragment;
import com.rockxin.cat.fragment.BidControlFragment;
import com.rockxin.cat.fragment.BidLogFragment;
import com.rockxin.cat.fragment.BidVideoFragment;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.utils.SPUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import rx.Subscriber;

public class BidDetailsActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.bidTitle)
    TextView bidTitle;
    @BindView(R.id.bitDate)
    TextView bitDate;
    @BindView(R.id.bidPrince)
    TextView bidPrince;
    @BindView(R.id.tv_completion)
    TextView tvCompletion;
    @BindView(R.id.tv_term)
    TextView tvTerm;
    @BindView(R.id.tv_charge_balance)
    TextView tvChargeBalance;
    @BindView(R.id.bidInfo)
    TextView bidInfo;
    @BindView(R.id.bidControl)
    TextView bidControl;
    @BindView(R.id.bidLog)
    TextView bidLog;
    @BindView(R.id.bidVideo)
    TextView bidVideo;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.bidBtn)
    TextView bidBtn;

    private BidDetailsActivity mBidDetailsActivity;

    private String targetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBidDetailsActivity = this;
        getData();
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_bid_details;
    }

    @Override
    public void initData() {
        title.setText("标的详情");
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(BIdInfoFragmentFragment.newInstance());
        fragmentList.add(BidControlFragment.newInstance());
        fragmentList.add(BidLogFragment.newInstance());
        fragmentList.add(BidVideoFragment.newInstance());
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), fragmentList));
        viewPager.setOffscreenPageLimit(fragmentList.size());
        viewPager.setNoScroll(true);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                JZVideoPlayer.releaseAllVideos();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @OnClick({R.id.back_img, R.id.bidInfo, R.id.bidControl, R.id.bidLog, R.id.bidVideo, R.id.bidBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.bidInfo:
                setDefaultState();
                bidInfo.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
                bidInfo.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorMoney));
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.bidControl:
                setDefaultState();
                bidControl.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
                bidControl.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorMoney));
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.bidLog:
                setDefaultState();
                bidLog.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
                bidLog.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorMoney));
                viewPager.setCurrentItem(2, false);
                break;
            case R.id.bidVideo:
                setDefaultState();
                bidVideo.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
                bidVideo.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorMoney));
                viewPager.setCurrentItem(3, false);
                break;
            case R.id.bidBtn:
                isLogin();
                break;
        }
    }

    private void setDefaultState() {
        bidInfo.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorTextColor));
        bidInfo.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
        bidLog.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorTextColor));
        bidLog.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
        bidControl.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorTextColor));
        bidControl.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
        bidVideo.setTextColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorTextColor));
        bidVideo.setBackgroundColor(ContextCompat.getColor(mBidDetailsActivity, R.color.colorWhite));
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    private void isLogin() {
        SPUtils spUtils = new SPUtils("Guide");
        String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
        if (token != null) {
            Intent intent = new Intent(this, BidActivity.class);
            intent.putExtra("targetId", targetId);
            startActivity(intent);
            return;
        }
        startActivity(new Intent(this, LoginActivity.class));
    }

    private void getData() {
        Logger.d("--------------------------------------------------");
        Intent getIntent = getIntent();
        targetId = getIntent.getStringExtra("targetId");
        CatNetWork catNetWork = new CatNetWork();
        catNetWork.dealview(targetId, new Subscriber<BidDetailsBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(BidDetailsBean bidDetailsBean) {
                Logger.json(new Gson().toJson(bidDetailsBean));
                if (bidDetailsBean.getStatus() == 1) {
                    initData(bidDetailsBean);
                }
            }
        });
    }

    private void initData(BidDetailsBean bidDetailsBean) {
        UseBean useBean = bidDetailsBean.getData();
        bidTitle.setText(useBean.getName());
        bitDate.setText("发布日期：" + useBean.getCreate_time());
        bidPrince.setText(useBean.getRate_foramt());
        tvCompletion.setText(useBean.getProgress_point() + "%");
        tvTerm.setText(useBean.getRepay_time_type());
        tvChargeBalance.setText("" + useBean.getShengyu_money());

        SPUtils spUtils = new SPUtils("Guide");
        spUtils.put(Constant.PREFERENCES.RATENUM, useBean.getRate_foramt());


//        EventBus.getDefault().post(data, "bank_list");
        EventBus.getDefault().post(bidDetailsBean.getLoad_list(), "bid_log");
//        EventBus.getDefault().post(data, "bank_list");
        EventBus.getDefault().post(useBean.getVideo(), "bid_video");


    }
}
